
import { onMounted, onUnmounted } from "vue";
import type { Router } from "vue-router";

export function onEnter(fn: () => void) {
  const listener = (e: KeyboardEvent) => {
    if (e.code === "Enter") {
      fn();
    }
  };

  onMounted(() => {
    document.addEventListener("keyup", listener);
  });

  onUnmounted(() => {
    document.removeEventListener("keyup", listener);
  });
}



export function useDetailId(router: Router) {
  let nextId = null;

  const getId = (idKey = "detailId") => {
    if (nextId !== null) {
      return nextId;
    }

    const id = router.currentRoute.value.query[idKey] as string;

    return typeof id === "string" ? id : null;
  };

  getId.setId = (next: any) => {
    nextId = next;
    return getId;
  };

  return getId;
}